package com.cs2340.taskplusplus;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;

/**
 * This activity is displayed when a user creates a new task
 * OR edits an old one.
 * 
 * It lets the user change the parameters of the task.
 * 
 * @author GRILLENBANNER
 *
 */
public class NewTaskScreen extends Activity implements OnClickListener {
	
	private TaskList parentList; //the task list that is to be the proud parent of the newly created task
	private Intent intent;
	private LocationManager locManager;
	private EditText mapEditText;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newtaskview);
        
        //get the parent list that has been passed in by name
        String parentListName = this.getIntent().getExtras().getString("Task List Name");
        parentList = new TaskList(parentListName);
        
        // auto fill current location
        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mapEditText = (EditText) findViewById(R.id.mapLocationDisplay_newTaskView);

        double lat = 33.7489;
        double lon = 84.3881;
		CurrentLocation.setLat((int)(lat*Math.pow(10,6)));
		CurrentLocation.setLon((int)(lon*Math.pow(10,6)));
        mapEditText.setText(String.valueOf(CurrentLocation.getLat())+", "+String.valueOf(CurrentLocation.getLon()));        
        
        
        //button listener making
        View cancelB = findViewById(R.id.cancelButton_newTaskView);
        cancelB.setOnClickListener(this);
        View submitB = findViewById(R.id.submitButton_newTaskView);
        submitB.setOnClickListener(this);
        View mapB = findViewById(R.id.mapButton_newTaskView);
        mapB.setOnClickListener(this);
        View mapUpdateB = findViewById(R.id.mapUpdateButton_newTaskView);
        mapUpdateB.setOnClickListener(this);
        
        
    }

    /**
     * Detects when the user clicks a button and reacts appropriately
     * 
     * @param View v The view that was clicked
     */
	public void onClick(View v) {
		switch(v.getId())
    	{
		// displays a map to select task location
    	case (R.id.mapButton_newTaskView):
			intent = new Intent(this, MapViewDisplay.class);	
    		startActivity(intent);
			break;
    	case (R.id.mapUpdateButton_newTaskView):
    		
    		mapEditText.setText(String.valueOf(CurrentLocation.getLat())+", "+String.valueOf(CurrentLocation.getLon()));
    		break;
    	case (R.id.cancelButton_newTaskView):
    		//acts like a press of the back button
    		onBackPressed();
    		break;
    	case (R.id.submitButton_newTaskView):
    		//make a new task
    		String name = ((EditText)findViewById(R.id.nameText_newTaskView)).getText().toString();
			String description = ((EditText)findViewById(R.id.descriptionText_newTaskView)).getText().toString();
			DatePicker dp = ((DatePicker)findViewById(R.id.datePicker_newTaskView));
			Date date = new Date(dp.getYear() - 1900, dp.getMonth(), dp.getDayOfMonth());
			CheckBox c = (CheckBox) findViewById(R.id.checkbox);

			String locStr = ((EditText)findViewById(R.id.mapLocationDisplay_newTaskView)).getText().toString();
			String latStr = "";
			String lonStr = "";
			while (locStr.charAt(0) != ','){
				latStr = latStr + locStr.charAt(0);
				locStr = locStr.substring(1);
			}
			lonStr = locStr.substring(2);
			Location loc = new Location(Integer.parseInt(latStr),Integer.parseInt(lonStr));
			
			/*
			 * GET THE CONSTRUCTOR OF TASK TO USE LOCATION
			 * THEN ADD loc TO THE BELOW CONSTRUCTOR OF newTask
			 */
			
			Doable newTask;
    		if (!c.isChecked())
    		{
    			newTask = new Task(parentList.getTableName(),name, description, date, null, loc);
    		}
    		else
    		{
    			newTask = new TaskList(parentList.getTableName(), name, description, date, null, loc);
    		}
    		if (!newTask.isList())
    			parentList.addTask(newTask);
    		//add this task to the tasklist
    		//return to the tasklistscreen with this new information
			this.finish();
			break;
    	}
	}
}
